﻿using Bangle.BLL;
using Bangle.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft;

public partial class Phone_PhoneEdit : BasePage
{
    #region 属性
    /// <summary>
    /// Guid
    /// </summary>
    private string id
    {
        get
        {
            return Request.QueryString["id"] == null ? "" : Request.QueryString["id"].ToString();
        }
    }

    /// <summary>
    /// Action
    /// </summary>
    public string Action
    {
        get
        {
            return Request.QueryString["Action"] == null ? "Add" : Request.QueryString["Action"].ToString();
        }
    }

    public string pageIndex
    {
        get
        {
            return Request.QueryString["pageIndex"] == null ? "0" : Request.QueryString["pageIndex"].ToString();
        }
    }
    #endregion

    T_Phone model = new T_Phone();
    T_PhoneBLL bll = new T_PhoneBLL();

    T_PhoneTypeBLL type_bll = new T_PhoneTypeBLL();
    T_SupplierBLL supplier_bll = new T_SupplierBLL();
    T_PhoneAndPhoneTypeBLL phoneAndPhoneType_bll = new T_PhoneAndPhoneTypeBLL();
    UtilityBLL u_bll = new UtilityBLL();
    T_PhonePictureBLL pic_bll = new T_PhonePictureBLL();
    T_ClassificationBLL class_bll = new T_ClassificationBLL();

    #region 加载
    protected void Page_Load(object sender, EventArgs e)
    {
        setUserControlStatus();

        if (!IsPostBack)
        {
            //绑定产品类型
            BindCheckBoxList();
            BindDropDownList();

            if (Action == "Modify" || Action == "Detail" || Action == "HDetail")
            {
                getDate(id);
            }

            SetControlStatus();
        }
    }

    private void SetControlStatus()
    {
        if (Action == "Detail" || Action == "HDetail")
        {
            this.ddl_SupplierId.Visible = false;
            this.txt_PartNo.Visible = false;
            this.txt_PCS.Visible = false;
            this.txt_RMB.Visible = false;
            this.txt_Specification.Visible = false;
            this.txt_USD.Visible = false;
            this.ddl_Chip.Visible = false;
            this.ddl_Screen.Visible = false;
            this.ddl_Series.Visible = false;
            this.cbl_PhoneType.Visible = false;
            this.txt_SalePoint.Visible = false;

            this.txt_PhoneCode.Visible = false;

            //this.btn_Modify.Visible = true;
            //this.btn_Save.Visible = false;


            this.ddl_System.Visible = false;
            this.ddl_Frequency.Visible = false;
            this.ddl_NetWork.Visible = false;
            this.ddl_SimType.Visible = false;
            this.ddl_ScreenPixel.Visible = false;
            this.ddl_RAM.Visible = false;
            this.ddl_ROM.Visible = false;
            this.ddl_CameraFront.Visible = false;
            this.ddl_CameraBack.Visible = false;
            this.ddl_BatteryType.Visible = false;
            this.ddl_Earphone.Visible = false;
            this.ddl_Charger.Visible = false;

            this.txt_Color.Visible = false;
            this.txt_Accessories.Visible = false;
            this.txt_Size.Visible = false;

            #region Label
            this.lbl_PhoneType.Visible = true;
            this.lbl_SupplierId.Visible = true;
            this.lbl_PartNo.Visible = true;
            this.lbl_PCS.Visible = true;
            this.lbl_RMB.Visible = true;
            this.lbl_USD.Visible = true;
            this.lbl_Chip.Visible = true;
            this.lbl_Screen.Visible = true;
            this.lbl_Series.Visible = true;
            this.lbl_SalePoint.Visible = true;
            this.lbl_PhoneCode.Visible = true;

            this.lbl_System.Visible = true;
            this.lbl_Frequency.Visible = true;
            this.lbl_NetWork.Visible = true;
            this.lbl_SimType.Visible = true;
            this.lbl_ScreenPixel.Visible = true;
            this.lbl_RAM.Visible = true;
            this.lbl_ROM.Visible = true;
            this.lbl_CameraFront.Visible = true;
            this.lbl_CameraBack.Visible = true;
            this.lbl_BatteryType.Visible = true;
            this.lbl_Earphone.Visible = true;
            this.lbl_Charger.Visible = true;

            this.lbl_Color.Visible = true;
            this.lbl_Accessories.Visible = true;
            this.lbl_Size.Visible = true;
            #endregion
        }
        else if (Action == "HAdd" || Action == "HModify" || Action == "Modify")
        {
            this.lbl_PhoneType.Visible = false;
            this.ddl_SupplierId.Visible = true;
            this.txt_PartNo.Visible = true;
            this.txt_PCS.Visible = true;
            this.txt_RMB.Visible = true;
            this.txt_Specification.Visible = true;
            this.txt_USD.Visible = true;
            this.ddl_Chip.Visible = true;
            this.ddl_Screen.Visible = true;
            this.ddl_Series.Visible = true;
            this.cbl_PhoneType.Visible = true;
            this.txt_SalePoint.Visible = true;

            this.txt_PhoneCode.Visible = true;

            //this.btn_Modify.Visible = false;
            //this.btn_Save.Visible = true;

            this.ddl_System.Visible = true;
            this.ddl_Frequency.Visible = true;
            this.ddl_NetWork.Visible = true;
            this.ddl_SimType.Visible = true;
            this.ddl_ScreenPixel.Visible = true;
            this.ddl_RAM.Visible = true;
            this.ddl_ROM.Visible = true;
            this.ddl_CameraFront.Visible = true;
            this.ddl_CameraBack.Visible = true;
            this.ddl_BatteryType.Visible = true;
            this.ddl_Earphone.Visible = true;
            this.ddl_Charger.Visible = true;

            this.txt_Color.Visible = true;
            this.txt_Accessories.Visible = true;
            this.txt_Size.Visible = true;

            #region Label
            this.lbl_SupplierId.Visible = false;
            this.lbl_PartNo.Visible = false;
            this.lbl_PCS.Visible = false;
            this.lbl_RMB.Visible = false;
            this.lbl_USD.Visible = false;
            this.lbl_Chip.Visible = false;
            this.lbl_Screen.Visible = false;
            this.lbl_Series.Visible = false;
            this.lbl_SalePoint.Visible = false;
            this.lbl_PhoneCode.Visible = false;

            this.lbl_System.Visible = false;
            this.lbl_Frequency.Visible = false;
            this.lbl_NetWork.Visible = false;
            this.lbl_SimType.Visible = false;
            this.lbl_ScreenPixel.Visible = false;
            this.lbl_RAM.Visible = false;
            this.lbl_ROM.Visible = false;
            this.lbl_CameraFront.Visible = false;
            this.lbl_CameraBack.Visible = false;
            this.lbl_BatteryType.Visible = false;
            this.lbl_Earphone.Visible = false;
            this.lbl_Charger.Visible = false;

            this.lbl_Color.Visible = false;
            this.lbl_Accessories.Visible = false;
            this.lbl_Size.Visible = false;
            #endregion
        }

        this.txt_Specification.Visible = true;
        if (Action == "HAdd" || Action == "Modify")
        {
            this.txt_Specification.Enabled = true;
        }
        else
        {
            this.txt_Specification.Enabled = false;
        }


        this.btn_Modify.Visible = (Action == "Detail");
        this.btn_Save.Visible = (Action == "HAdd" || Action == "Modify");
        this.btn_Back.Visible = (Action != "HAdd" && Action != "HDetail");
    }
    #endregion

    #region 检验用户权限
    private void setUserControlStatus()
    {
        //检验用户权限
        base.CheckUserPermission("002002");
        this.btn_Modify.Enabled = base.CheckUserPermissionOperation("002002", "Add");
        this.btn_Save.Enabled = base.CheckUserPermissionOperation("002002", "Add") || base.CheckUserPermissionOperation("002002", "Edit");
    }
    #endregion

    #region 绑定数据
    private void BindCheckBoxList()
    {
        List<T_PhoneType> type_list = new List<T_PhoneType>();
        type_list = type_bll.GetModelList("Valid=1 order by PhoenTypeName");

        this.cbl_PhoneType.DataTextField = "PhoenTypeName";
        this.cbl_PhoneType.DataValueField = "PhoneTypeId";
        this.cbl_PhoneType.DataSource = type_list;
        this.cbl_PhoneType.DataBind();
    }

    private void BindDropDownList()
    {
        DataTable dt = u_bll.GetListByPage("T_Supplier", "SupplierId,SupplierName", "SupplierName", "Valid=1");

        this.ddl_SupplierId.DataTextField = "SupplierName";
        this.ddl_SupplierId.DataValueField = "SupplierId";
        this.ddl_SupplierId.DataSource = dt;
        this.ddl_SupplierId.DataBind();
        this.ddl_SupplierId.Items.Insert(0, new ListItem("<无>", ""));

        //类型
        List<T_Classification> class_list = class_bll.GetModelList("1=1");

        //屏幕
        int screen_id = class_list.Find(o => o.ClassificationCode == "Screen").ClassificationId;
        List<T_Classification> screen_list = class_list.FindAll(o => o.ParentId == screen_id);
        this.ddl_Screen.DataTextField = "ClassificationName";
        this.ddl_Screen.DataValueField = "ClassificationId";
        this.ddl_Screen.DataSource = screen_list;
        this.ddl_Screen.DataBind();
        this.ddl_Screen.Items.Insert(0, new ListItem("<无>", "0"));

        //芯片
        int ship_id = class_list.Find(o => o.ClassificationCode == "Chip").ClassificationId;
        List<T_Classification> chip_list = class_list.FindAll(o => o.ParentId == ship_id);
        this.ddl_Chip.DataTextField = "ClassificationName";
        this.ddl_Chip.DataValueField = "ClassificationId";
        this.ddl_Chip.DataSource = chip_list;
        this.ddl_Chip.DataBind();
        this.ddl_Chip.Items.Insert(0, new ListItem("<无>", "0"));

        //系列
        int series_id = class_list.Find(o => o.ClassificationCode == "Series").ClassificationId;
        List<T_Classification> series_list = class_list.FindAll(o => o.ParentId == series_id);
        this.ddl_Series.DataTextField = "ClassificationName";
        this.ddl_Series.DataValueField = "ClassificationId";
        this.ddl_Series.DataSource = series_list;
        this.ddl_Series.DataBind();
        this.ddl_Series.Items.Insert(0, new ListItem("<无>", "0"));

        //系统
        int system_id = class_list.Find(o => o.ClassificationCode == "System").ClassificationId;
        List<T_Classification> system_list = class_list.FindAll(o => o.ParentId == series_id);
        this.ddl_System.DataTextField = "ClassificationName";
        this.ddl_System.DataValueField = "ClassificationId";
        this.ddl_System.DataSource = system_list;
        this.ddl_System.DataBind();
        this.ddl_System.Items.Insert(0, new ListItem("<无>", "0"));

        //频段	Frequency
        int Frequency_id = class_list.Find(o => o.ClassificationCode == "Frequency").ClassificationId;
        List<T_Classification> Frequency_list = class_list.FindAll(o => o.ParentId == Frequency_id);
        this.ddl_Frequency.DataTextField = "ClassificationName";
        this.ddl_Frequency.DataValueField = "ClassificationId";
        this.ddl_Frequency.DataSource = Frequency_list;
        this.ddl_Frequency.DataBind();
        this.ddl_Frequency.Items.Insert(0, new ListItem("<无>", "0"));

        //网络	NetWork
        int NetWork_id = class_list.Find(o => o.ClassificationCode == "NetWork").ClassificationId;
        List<T_Classification> NetWork_list = class_list.FindAll(o => o.ParentId == NetWork_id);
        this.ddl_NetWork.DataTextField = "ClassificationName";
        this.ddl_NetWork.DataValueField = "ClassificationId";
        this.ddl_NetWork.DataSource = NetWork_list;
        this.ddl_NetWork.DataBind();
        this.ddl_NetWork.Items.Insert(0, new ListItem("<无>", "0"));


        //卡槽	SIM Type
        int SimType_id = class_list.Find(o => o.ClassificationCode == "SimType").ClassificationId;
        List<T_Classification> SimType_list = class_list.FindAll(o => o.ParentId == SimType_id);
        this.ddl_SimType.DataTextField = "ClassificationName";
        this.ddl_SimType.DataValueField = "ClassificationId";
        this.ddl_SimType.DataSource = SimType_list;
        this.ddl_SimType.DataBind();
        this.ddl_SimType.Items.Insert(0, new ListItem("<无>", "0"));

        //屏幕像素	Screen Pixel
        int ScreenPixel_id = class_list.Find(o => o.ClassificationCode == "ScreenPixel").ClassificationId;
        List<T_Classification> ScreenPixel_list = class_list.FindAll(o => o.ParentId == ScreenPixel_id);
        this.ddl_ScreenPixel.DataTextField = "ClassificationName";
        this.ddl_ScreenPixel.DataValueField = "ClassificationId";
        this.ddl_ScreenPixel.DataSource = ScreenPixel_list;
        this.ddl_ScreenPixel.DataBind();
        this.ddl_ScreenPixel.Items.Insert(0, new ListItem("<无>", "0"));

        //内存RAM	RAM
        int RAM_id = class_list.Find(o => o.ClassificationCode == "RAM").ClassificationId;
        List<T_Classification> RAM_list = class_list.FindAll(o => o.ParentId == RAM_id);
        this.ddl_RAM.DataTextField = "ClassificationName";
        this.ddl_RAM.DataValueField = "ClassificationId";
        this.ddl_RAM.DataSource = RAM_list;
        this.ddl_RAM.DataBind();
        this.ddl_RAM.Items.Insert(0, new ListItem("<无>", "0"));

        //内存ROM	ROM
        int ROM_id = class_list.Find(o => o.ClassificationCode == "ROM").ClassificationId;
        List<T_Classification> ROM_list = class_list.FindAll(o => o.ParentId == ROM_id);
        this.ddl_ROM.DataTextField = "ClassificationName";
        this.ddl_ROM.DataValueField = "ClassificationId";
        this.ddl_ROM.DataSource = ROM_list;
        this.ddl_ROM.DataBind();
        this.ddl_ROM.Items.Insert(0, new ListItem("<无>", "0"));

        //摄像头前	CameraFront
        int CameraFront_id = class_list.Find(o => o.ClassificationCode == "CameraFront").ClassificationId;
        List<T_Classification> CameraFront_list = class_list.FindAll(o => o.ParentId == CameraFront_id);
        this.ddl_CameraFront.DataTextField = "ClassificationName";
        this.ddl_CameraFront.DataValueField = "ClassificationId";
        this.ddl_CameraFront.DataSource = CameraFront_list;
        this.ddl_CameraFront.DataBind();
        this.ddl_CameraFront.Items.Insert(0, new ListItem("<无>", "0"));

        //摄像头后	CameraBack
        int CameraBack_id = class_list.Find(o => o.ClassificationCode == "CameraBack").ClassificationId;
        List<T_Classification> CameraBack_list = class_list.FindAll(o => o.ParentId == CameraBack_id);
        this.ddl_CameraBack.DataTextField = "ClassificationName";
        this.ddl_CameraBack.DataValueField = "ClassificationId";
        this.ddl_CameraBack.DataSource = CameraBack_list;
        this.ddl_CameraBack.DataBind();
        this.ddl_CameraBack.Items.Insert(0, new ListItem("<无>", "0"));

        //电池	BatteryType
        int BatteryType_id = class_list.Find(o => o.ClassificationCode == "BatteryType").ClassificationId;
        List<T_Classification> BatteryType_list = class_list.FindAll(o => o.ParentId == BatteryType_id);
        this.ddl_BatteryType.DataTextField = "ClassificationName";
        this.ddl_BatteryType.DataValueField = "ClassificationId";
        this.ddl_BatteryType.DataSource = BatteryType_list;
        this.ddl_BatteryType.DataBind();
        this.ddl_BatteryType.Items.Insert(0, new ListItem("<无>", "0"));

        //耳机	Earphone
        int Earphone_id = class_list.Find(o => o.ClassificationCode == "Earphone").ClassificationId;
        List<T_Classification> Earphone_list = class_list.FindAll(o => o.ParentId == Earphone_id);
        this.ddl_Earphone.DataTextField = "ClassificationName";
        this.ddl_Earphone.DataValueField = "ClassificationId";
        this.ddl_Earphone.DataSource = Earphone_list;
        this.ddl_Earphone.DataBind();
        this.ddl_Earphone.Items.Insert(0, new ListItem("<无>", "0"));

        //充电器	Charger
        int Charger_id = class_list.Find(o => o.ClassificationCode == "Charger").ClassificationId;
        List<T_Classification> Charger_list = class_list.FindAll(o => o.ParentId == Charger_id);
        this.ddl_Charger.DataTextField = "ClassificationName";
        this.ddl_Charger.DataValueField = "ClassificationId";
        this.ddl_Charger.DataSource = Charger_list;
        this.ddl_Charger.DataBind();
        this.ddl_Charger.Items.Insert(0, new ListItem("<无>", "0"));

        //颜色	Color
        //配件	Accessories
        //尺寸	Size

    }

    /// <summary>
    /// 加载数据
    /// </summary>
    /// <param name="id"></param>
    /// 

    private void getDate(string id)
    {
        model = bll.GetModel(new Guid(id));
        if (model == null)
        {
            return;
        }

        this.ddl_SupplierId.SelectedValue = model.SupplierId.ToString();
        this.txt_PartNo.Text = model.PartNo;
        this.txt_PCS.Text = model.PCS.ToString("0.00");
        this.txt_RMB.Text = model.RMB.ToString("0.00");
        this.txt_Specification.Text = model.Specification;
        //this.lbl_Specification.Text = this.txt_Specification.Text;
        this.txt_USD.Text = model.USD.ToString("0.00");
        this.txt_SalePoint.Text = model.SalePoint;

        this.txt_PhoneCode.Text = model.PhoneCode;

        this.ch_Valid.Checked = model.Valid == 1;

        this.ddl_Chip.SelectedValue = model.ChipId.ToString();
        this.ddl_Screen.SelectedValue = model.ScreenId.ToString();
        this.ddl_Series.SelectedValue = model.SeriesId.ToString();

        this.ddl_System.SelectedValue = model.SystemId.ToString();
        this.ddl_Frequency.SelectedValue = model.FrequencyId.ToString();
        this.ddl_NetWork.SelectedValue = model.NetWorkId.ToString();
        this.ddl_SimType.SelectedValue = model.SimTypeId.ToString();
        this.ddl_ScreenPixel.SelectedValue = model.ScreenPixelId.ToString();
        this.ddl_RAM.SelectedValue = model.RAMId.ToString();
        this.ddl_ROM.SelectedValue = model.ROMId.ToString();
        this.ddl_CameraFront.SelectedValue = model.CameraFrontId.ToString();
        this.ddl_CameraBack.SelectedValue = model.CameraBackId.ToString();
        this.ddl_BatteryType.SelectedValue = model.BatteryTypeId.ToString();
        this.ddl_Earphone.SelectedValue = model.EarphoneId.ToString();
        this.ddl_Charger.SelectedValue = model.ChargerId.ToString();

        this.txt_Color.Text = model.Color;
        this.txt_Accessories.Text = model.Accessories;
        this.txt_Size.Text = model.Size;



        #region 绑定产品类型
        string _queryString = string.Format(" PhoneId='{0}'", id);
        List<T_PhoneAndPhoneType> selected_list = phoneAndPhoneType_bll.GetModelList(_queryString);
        for (int i = 0; i < this.cbl_PhoneType.Items.Count; i++)
        {
            bool isSelected = (selected_list.Count(o => o.PhoneTypeId == new Guid(this.cbl_PhoneType.Items[i].Value)) > 0);
            this.cbl_PhoneType.Items[i].Selected = isSelected;
        }
        #endregion

        #region 为Label赋值
        this.lbl_PartNo.Text = this.txt_PartNo.Text;
        this.lbl_PCS.Text = this.txt_PCS.Text;
        this.lbl_RMB.Text = this.txt_RMB.Text;
        this.lbl_USD.Text = this.txt_USD.Text;
        this.lbl_SalePoint.Text = this.txt_SalePoint.Text.Replace("\r", "").Replace("\n", "<br/>");

        this.lbl_PhoneCode.Text = this.txt_PhoneCode.Text;

        this.lbl_Chip.Text = this.ddl_Chip.SelectedItem.Text;
        this.lbl_Screen.Text = this.ddl_Screen.SelectedItem.Text;
        this.lbl_Series.Text = this.ddl_Series.SelectedItem.Text;
        this.lbl_SupplierId.Text = this.ddl_SupplierId.SelectedItem.Text;



        this.lbl_System.Text = this.ddl_System.SelectedItem.Text;
        this.lbl_Frequency.Text = this.ddl_Frequency.SelectedItem.Text;
        this.lbl_NetWork.Text = this.ddl_NetWork.SelectedItem.Text;
        this.lbl_SimType.Text = this.ddl_SimType.SelectedItem.Text;
        this.lbl_ScreenPixel.Text = this.ddl_ScreenPixel.SelectedItem.Text;
        this.lbl_RAM.Text = this.ddl_RAM.SelectedItem.Text;
        this.lbl_ROM.Text = this.ddl_ROM.SelectedItem.Text;
        this.lbl_CameraFront.Text = this.ddl_CameraFront.SelectedItem.Text;
        this.lbl_CameraBack.Text = this.ddl_CameraBack.SelectedItem.Text;
        this.lbl_BatteryType.Text = this.ddl_BatteryType.SelectedItem.Text;
        this.lbl_Earphone.Text = this.ddl_Earphone.SelectedItem.Text;
        this.lbl_Charger.Text = this.ddl_Charger.SelectedItem.Text;
             
        this.lbl_Color.Text = this.txt_Color.Text;
        this.lbl_Accessories.Text = this.txt_Accessories.Text;
        this.lbl_Size.Text = this.txt_Size.Text;

        string sPhoneTypeHtml = "";
        foreach (ListItem item in this.cbl_PhoneType.Items)
        {
            if (item.Selected)
            {
                sPhoneTypeHtml += string.Format("<span>{0}</span>", item.Text);
            }
        }
        this.lbl_PhoneType.Text = sPhoneTypeHtml;
        this.cbl_PhoneType.Visible = true;
        #endregion

        #region 绑定图片
        List<T_PhonePicture> pic_list = (List<T_PhonePicture>)pic_bll.GetModelList(string.Format(" PhoneId='{0}' order by IsTop desc", id));
        List<PicJson> picJson_list = new List<PicJson>();
        foreach (T_PhonePicture item in pic_list)
        {
            picJson_list.Add(new PicJson() { id = item.PhonePictureId.ToString(), url = item.Src });
        }

        this.hf_ProductPicture.Value = Newtonsoft.Json.JsonConvert.SerializeObject(picJson_list);
        #endregion
    }
    #endregion

    #region 提交
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        string sPartNo = this.txt_PartNo.Text.Trim();
        int iCheck = 0;
        string sCheckSql = string.Format(" PartNo='{0}' and ltrim(PhoneId)<>'{1}' ", sPartNo, id);
        iCheck = bll.GetModelList(sCheckSql).Count;
        if (iCheck > 0)
        {
            base.Alert(string.Format("PART NO.【{0}】 已存在，请重新输入！", sPartNo));
            return;
        }

        if (Action == "Modify")
        {
            model = bll.GetModel(new Guid(id));
        }
        else if (Action == "HAdd")
        {
            model = new T_Phone();
        }
        else
        {
            base.Alert("异常操作！");
            return;
        }

        //model.LogoPicture = "";
        model.PartNo = this.txt_PartNo.Text.Trim();
        model.PCS = this.txt_PCS.Text.Trim() == "" ? 0 : Convert.ToDecimal(this.txt_PCS.Text.Trim());
        model.PhoneCode = this.txt_PhoneCode.Text.Trim();
        //model.PhoneId=
        model.PhoneName = "";
        model.RMB = this.txt_RMB.Text.Trim() == "" ? 0 : Convert.ToDecimal(this.txt_RMB.Text.Trim());
        model.USD = this.txt_USD.Text.Trim() == "" ? 0 : Convert.ToDecimal(this.txt_USD.Text.Trim());
        model.Specification = this.txt_Specification.Text.Trim();
        model.SupplierId = this.ddl_SupplierId.SelectedValue == "" ? Guid.Empty : Guid.Parse(this.ddl_SupplierId.SelectedValue);
        model.UpdateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        model.UpdateUserId = base.CurrentSession.UserID;
        model.Valid = this.ch_Valid.Checked ? 1 : 0;

        model.ScreenId = Convert.ToInt32(this.ddl_Screen.SelectedValue);
        model.SeriesId = Convert.ToInt32(this.ddl_Series.SelectedValue);
        model.ChipId = Convert.ToInt32(this.ddl_Chip.SelectedValue);
        model.SalePoint = this.txt_SalePoint.Text.Trim();


        model.SystemId = Convert.ToInt32(this.ddl_System.SelectedValue);
        model.FrequencyId = Convert.ToInt32(this.ddl_Frequency.SelectedValue);
        model.NetWorkId = Convert.ToInt32(this.ddl_NetWork.SelectedValue);
        model.SimTypeId = Convert.ToInt32(this.ddl_SimType.SelectedValue);
        model.ScreenPixelId = Convert.ToInt32(this.ddl_ScreenPixel.SelectedValue);
        model.RAMId = Convert.ToInt32(this.ddl_RAM.SelectedValue);
        model.ROMId = Convert.ToInt32(this.ddl_ROM.SelectedValue);
        model.CameraFrontId = Convert.ToInt32(this.ddl_CameraFront.SelectedValue);
        model.CameraBackId = Convert.ToInt32(this.ddl_CameraBack.SelectedValue);
        model.BatteryTypeId = Convert.ToInt32(this.ddl_BatteryType.SelectedValue);
        model.EarphoneId = Convert.ToInt32(this.ddl_Earphone.SelectedValue);
        model.ChargerId = Convert.ToInt32(this.ddl_Charger.SelectedValue);

        model.Color = this.txt_Color.Text;
        model.Accessories = this.txt_Accessories.Text;
        model.Size = this.txt_Size.Text;

        try
        {
            if (Action == "Modify")
            {
                //修改
                bll.Update(model);

            }
            else if (Action == "HAdd")
            {
                model.CreateTime = model.UpdateTime;
                model.CreateUserId = model.UpdateUserId;
                model.PhoneId = Guid.NewGuid();

                //保存
                bll.Add(model);
            }

            #region 保存产品类型
            List<T_PhoneAndPhoneType> add_list = new List<T_PhoneAndPhoneType>();
            List<T_PhoneAndPhoneType> edit_list = new List<T_PhoneAndPhoneType>();

            for (int i = 0; i < this.cbl_PhoneType.Items.Count; i++)
            {
                Guid _phoneTypeId = new Guid(this.cbl_PhoneType.Items[i].Value);
                string _querySql = string.Format(" PhoneId='{0}' and PhoneTypeId='{1}'", model.PhoneId, _phoneTypeId);
                if (this.cbl_PhoneType.Items[i].Selected)
                {

                    T_PhoneAndPhoneType _item = phoneAndPhoneType_bll.GetModelList(_querySql).FirstOrDefault();

                    if (_item == null)
                    {
                        _item = new T_PhoneAndPhoneType();
                        _item.PhoneId = model.PhoneId;
                        _item.PhoneTypeId = _phoneTypeId;
                        phoneAndPhoneType_bll.Add(_item);
                    }
                    else
                    {
                        _item.PhoneTypeId = _phoneTypeId;
                        phoneAndPhoneType_bll.Update(_item);
                    }
                }
                else
                {
                    phoneAndPhoneType_bll.Delete(_querySql);
                }
            }
            #endregion

            #region 保存图片
            List<PicJson> picJson_list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<PicJson>>(this.hf_ProductPicture.Value);
            //删除
            string strDelSql = "''";
            if (picJson_list != null)
            {
                foreach (PicJson item in picJson_list)
                {
                    if (item.id != "")
                    {
                        strDelSql += ",";
                        strDelSql += string.Format("'{0}'", item.id);
                    }
                }
            }
            pic_bll.Delete(string.Format(" PhoneId='{0}' and ltrim(PhonePictureId) not in ({1})", model.PhoneId, strDelSql));

            int isTop = 1;
            if (picJson_list != null)
            {
                foreach (PicJson item in picJson_list)
                {
                    T_PhonePicture pic = new T_PhonePicture();
                    pic.PhonePictureId = item.id.Length > 10 ? Guid.Parse(item.id) : Guid.NewGuid();
                    pic.PhoneId = model.PhoneId;
                    pic.Src = item.url;
                    pic.IsTop = isTop;


                    if (item.id.Length < 10)
                    {
                        //新增
                        pic_bll.Add(pic);
                    }
                    else
                    {
                        //修改
                        pic_bll.Update(pic);
                    }
                    isTop = 0;
                }
            }

            #endregion

            if (Action == "HAdd")
            {
                //base.Redirect("操作成功！", "PhoneEdit.aspx?Action=HDetail&id=" + model.PhoneId);
                base.Redirect("操作成功！", "PhoneEdit.aspx?Action=HAdd&id=" + model.PhoneId);
            }
            else
            {
                //base.Redirect("操作成功！", "PhoneEdit.aspx?Action=Detail&id=" + model.PhoneId);
                base.CloseWindowAndRefresh();
            }
        }
        catch (Exception ex)
        {
            base.AlertError(ex.ToString());
        }
    }
    #endregion

    #region 编辑按钮
    protected void btn_Modify_Click(object sender, EventArgs e)
    {
        Response.Redirect(string.Format("PhoneEdit.aspx?Action=Modify&Id={0}&pageIndex={1}", id, pageIndex));
    }
    #endregion

    #region 图片
    private class PicJson
    {
        public string id { get; set; }
        public string url { get; set; }
    }
    #endregion
}